﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TienIch.Utils;
using DevExpress.XtraEditors;

namespace TienIch
{
    public partial class frmKetNoi : FormBase
    {   
        public frmKetNoi()
        {
            InitializeComponent();
        }

        public static string passPhrase = "Pas5pr@se";        // can be any string
        public static string saltValue = "s@1tValue";        // can be any string
        public static string hashAlgorithm = "SHA1";             // can be "MD5"
        public static int passwordIterations = 2;                  // can be any number
        public static string initVector = "@CSS@CSS@CSS@CSS"; // must be 16 bytes
        public static int keySize = 256;
        string server, data, user, pass;
        private void btnCreate_Click(object sender, EventArgs e)
        {
            server = DBUtils.Encrypt(txtServer.Text,
                                                   passPhrase,
                                                   saltValue,
                                                   hashAlgorithm,
                                                   passwordIterations,
                                                   initVector,
                                                   keySize);
            data = DBUtils.Encrypt(cmbdatabase.Text,
                                                   passPhrase,
                                                   saltValue,
                                                   hashAlgorithm,
                                                   passwordIterations,
                                                   initVector,
                                                   keySize);
            user = DBUtils.Encrypt(txtName.Text,
                                                   passPhrase,
                                                   saltValue,
                                                   hashAlgorithm,
                                                   passwordIterations,
                                                   initVector,
                                                   keySize);
            pass = DBUtils.Encrypt(txtPass.Text,
                                                   passPhrase,
                                                   saltValue,
                                                   hashAlgorithm,
                                                   passwordIterations,
                                                   initVector,
                                                   keySize);
            //System.IO.StreamWriter ghiFile = new System.IO.StreamWriter("default.ini");
            if (!File.Exists("default.ini"))//kiểm tra nếu chưa có file Pass.txt thì tạo ra file Pass.txt
            {
                FileStream fs;
                fs = new FileStream("default.ini", FileMode.Create);//Tạo file mới tên là Pass.txt
                StreamWriter sWriter = new StreamWriter(fs, Encoding.UTF8);

                sWriter.WriteLine(server);
                sWriter.WriteLine(data);
                sWriter.WriteLine(user);
                sWriter.WriteLine(pass);
                sWriter.Flush();
                fs.Close();
            }
            else
            {
                FileStream fs;
                fs = new FileStream("default.ini", FileMode.Open);//Tạo file mới tên là Pass.txt
                StreamWriter sWriter = new StreamWriter(fs, Encoding.UTF8);

                sWriter.WriteLine(server);
                sWriter.WriteLine(data);
                sWriter.WriteLine(user);
                sWriter.WriteLine(pass);
                sWriter.Flush();
                fs.Close();
            }
            XtraMessageBox.Show("Thành công");
        }

        private void frmKetNoi_Load(object sender, EventArgs e)
        {
            cmbAuthentication.SelectedIndex = 1;
            cmbAuthentication.Enabled = false;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            if (cmbAuthentication.SelectedIndex == 0)
            {
                cmbdatabase.Items.Clear();
                SqlConnection m_Connect = new SqlConnection("Data Source=" + txtServer.Text + ";Initial Catalog=master;Integrated Security=True;");
                SqlCommand m_Cmd = new SqlCommand("SP_DATABASES", m_Connect);
                SqlDataReader m_DReader;
                try
                {
                    m_Connect.Open();
                    m_DReader = m_Cmd.ExecuteReader();
                    while (m_DReader.Read())
                    {
                        cmbdatabase.Items.Add(m_DReader[0].ToString());
                    }
                    XtraMessageBox.Show("Kết nối thành công!", "Succecced", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (SqlException sqlEx)
                {
                    XtraMessageBox.Show(sqlEx.Message, "FAILED", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally
                {
                    if (m_Connect.State == ConnectionState.Open)
                        m_Connect.Close();
                    m_Connect.Dispose();
                    m_Cmd.Dispose();
                }
            }
            if (cmbAuthentication.SelectedIndex == 1)
            {
                cmbdatabase.Items.Clear();
                SqlConnection m_Conn = new SqlConnection("Data Source=" + txtServer.Text + ";Initial Catalog=master;User Id=" + txtName.Text + ";Password=" + txtPass.Text + ";");
                SqlCommand m_Cmd = new SqlCommand("SP_DATABASES", m_Conn);
                SqlDataReader m_DReader;
                try
                {
                    m_Conn.Open();
                    m_DReader = m_Cmd.ExecuteReader();
                    while (m_DReader.Read())
                    {
                        cmbdatabase.Items.Add(m_DReader[0].ToString());
                    }
                    XtraMessageBox.Show("Kết nối thành công!", "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (SqlException sqlEx)
                {
                    XtraMessageBox.Show(sqlEx.Message, "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally
                {
                    if (m_Conn.State == ConnectionState.Open)
                        m_Conn.Close();
                    m_Conn.Dispose();
                    m_Cmd.Dispose();
                }
            }
        }

        private void cmbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAuthentication.SelectedIndex == 0)
            {
                txtName.Enabled = false;
                txtPass.Enabled = false;
            }
            else
            {
                txtName.Enabled = true;
                txtPass.Enabled = true;
            }
        }
    }
}
